package com.ophiux.utils.common;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


public class ShiftUtils<T> {
	/**
	 * 将Map集合转换为object 对象
	 * 
	 * @param map
	 * @return
	 */
	public static <t> Object mapToObj(Map<String, Object> map,
			Class classT) {
		try {
			return transfer(map, classT.newInstance());
		} catch (InstantiationException ex) {
			ex.printStackTrace();
			System.out.println("what the fuck1?");
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
			System.out.println("what the fuck2?");
		}
		return null;
	}

	private static <t> Object transfer(Map<String, Object> map, Object obj) {

		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();

			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				if (map.containsKey(key)) {
					Object value = map.get(key);
					// 得到property对应的setter方法
					Method setter = property.getWriteMethod();
					try {
						setter.invoke(obj, value);
					} catch (IllegalArgumentException ex) {
						System.out.println("what the fuck?");
					}
				}
			}
		} catch (Exception ex) {
			System.out.println("what the fuck?");
		}

		return  obj;
	}

	
	/**
	 * 将对象转换为map
	 * @param object
	 * @return
	 */
	public static HashMap<String, String> objToMap(Object object) {
		if (object == null) {
			return null;
		}
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				// 过滤class属性
				if (key.equals("class")) {
					continue;
				}

				// 得到property对应的getter方法
				Method getter = property.getReadMethod();
				String value = (String) getter.invoke(object);
				if(StringUtil.isEmpty(value)) {
					value = "";
				}
				map.put(key, value);
			}
		} catch (Exception e) {
			System.out.println("transBean2Map Error " + e);
		}
		return map;
	}
	
	/**
	 * 将对象转换为map
	 * 2019-01-14 sqx 支持list对象
	 * @param object
	 * @return
	 */
	public static HashMap<String, Object> objToMapNew(Object object) {
		if (object == null) {
			return null;
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName().toUpperCase();
				
				//避免首字母转换
				/*if (key.length() <= 1 ){//只存在一个字符   本身是大写就转换为大写  本身是小写就转换为小写
					char chars[] = key.toCharArray();
					if(!Character.isUpperCase(key.charAt(0))) {
						chars[0] = Character.toUpperCase(chars[0]);
		        	}else {
		        		chars[0] = Character.toLowerCase(chars[0]);
		        	}
			        
			        key = new String(chars);
		        }else {
		        	if(!Character.isUpperCase(key.charAt(1))) {
		        		
		        	}
                    
		        }*/
				
				
				// 过滤class属性
				if (key.equals("class")) {
					continue;
				}

				// 得到property对应的getter方法
				Method getter = property.getReadMethod();
				
				Object o = getter.invoke(object);
				 if(o instanceof List) {
					List list = (List) o;
					if(list != null && list.size() != 0) {
						
					}else {
						o = new ArrayList();
					}
				}else {
//					if(o instanceof String) {
						String value = (String) o;
						if(StringUtil.isEmpty(value)) {
							o = "";
						}
//					}
				}
				
				map.put(key, o);
			}
		} catch (Exception e) {
			System.out.println("transBean2Map Error " + e);
		}
		return map;
	}

	/**
	 * 将对象转换为新的对象
	 * @param obj 转换前的对象
	 * @param Class 转换后的对象类
	 * @return
	 */
	public static Object objToNewObj(Object obj ,Class classObj) {
		Map objMap = objToMap(obj);
		Object returnobj = mapToObj(objMap, classObj);
		return returnobj;
	}
	
	
	
	/**
	 * 将对象转换为新的对象
	 * @param obj 转换前的对象
	 * @param Class 转换后的对象类
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List objToNewObjList(List objS ,Class classObj) {
		List obS = new ArrayList();
		for (Object obj : objS) {
			Map objMap = objToMap(obj);
			Object returnobj = mapToObj(objMap, classObj);
			obS.add(returnobj);
		}
		return obS;
	}
	

	public static void main(String[] args) {
	}
	
}
